package com.stupidzhe.vbreview.service.impl;


import com.stupidzhe.vbreview.bean.PageBean;
import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.dao.ClassMapper;
import com.stupidzhe.vbreview.dao.ProjectClassRecordMapper;
import com.stupidzhe.vbreview.dao.ProjectMapper;
import com.stupidzhe.vbreview.domain.Class;
import com.stupidzhe.vbreview.domain.Project;
import com.stupidzhe.vbreview.domain.ProjectClassRecord;
import com.stupidzhe.vbreview.service.ProjectService;
import com.stupidzhe.vbreview.util.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Time;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    private final ProjectMapper projectMapper;
    private final ClassMapper classMapper;
    private final ProjectClassRecordMapper projectClassRecordMapper;

    public ProjectServiceImpl(ProjectMapper projectMapper, ClassMapper classMapper, ProjectClassRecordMapper projectClassRecordMapper) {
        this.projectMapper = projectMapper;
        this.classMapper = classMapper;
        this.projectClassRecordMapper = projectClassRecordMapper;
    }

    @Override
    public PageBean<Project> getProjectPage(Integer pageNum, Integer teacherId, String keyword) {
        if (pageNum < 1) pageNum = 1;
        PageBean<Project> pageBean = new PageBean<>();
        pageBean.setPageNum(pageNum);
        List<Project> res = projectMapper.getPageContent(pageBean.getStart(), pageBean.getSize(), keyword, teacherId, null);
        pageBean.setRowCount(projectMapper.getCount(keyword, teacherId, null));
        pageBean.setContent(res);
        return pageBean;
    }

    @Override
    public ResultBean add(String projectName, Integer classification, String expression,
                          String demoLocation, String docLocation, Integer upTimesMax, Integer teacherId) {
        Project project = new Project();
        project.setStatus("1");
        project.setDemoLocation(demoLocation);
        project.setDocLocation(docLocation);
        project.setClassification(String.valueOf(classification));
        project.setProjectName(projectName);
        project.setProjectExpression(expression);
        project.setUpTime(TimeUtil.getCurrentTime());
        project.setTeacherId(teacherId);
        project.setUpTimesMax(upTimesMax);
        try {
            projectMapper.insert(project);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBean(false, "添加失败");
        }
        return new ResultBean(true, "添加成功");
    }

    @Override
    @Transactional
    public ResultBean editProject(Integer projectId, String projectName,
                                  String docLocation, String demoLocation, String expression,
                                  Integer teacherId, List<String> classList, String status, String upTimesMax) throws Exception {
        Project p = projectMapper.selectByPrimaryKeyForUpdate(projectId);
        if (p == null) {
            return new ResultBean(false, "作业编号不存在");
        }
        if (!teacherId.equals(p.getTeacherId())) {
            return new ResultBean(false, "无权限");
        }
        if (projectName != null) {
            if (projectName.trim().equals("")) {
                return new ResultBean(false, "作业名称不能为空");
            }
            Project p2 = projectMapper.selectByProjectName(projectName, projectId);

            if (p.getProjectName().equals(projectName)) {
                return new ResultBean(true, "与修改前一致");
            }
            if (p2 != null) {
                return new ResultBean(false, "作业名称重复");
            }
        }
        try {
            p.setProjectName(projectName);
            p.setDocLocation(docLocation);
            p.setDemoLocation(demoLocation);
            p.setProjectExpression(expression);
            p.setStatus(status);
            if (upTimesMax != null) {
                p.setUpTimesMax(Integer.valueOf(upTimesMax));
            }
            projectMapper.updateByPrimaryKeySelective(p);
        } catch (Exception e) {
            throw new Exception("更新失败");
        }
        if (classList != null)
            for (String classIdc : classList) {
                String[] classSplit = classIdc.split("[:]");
                Class c = classMapper.selectByPrimaryKey(Integer.valueOf(classSplit[0]));
                if (c == null) {
                    throw new Exception("更新失败");
                }
                ProjectClassRecord projectClassRecord;
                if (null != (projectClassRecord = projectClassRecordMapper.selectClassIdAndProjectId(Integer.valueOf(classSplit[0]), projectId))) {
                    if (Boolean.valueOf(classSplit[1])) {
                        projectClassRecord.setStatus("1");
                    } else {
                        projectClassRecord.setStatus("0");
                    }
                    projectClassRecordMapper.updateByPrimaryKey(projectClassRecord);
                } else {
                    projectClassRecord = new ProjectClassRecord();
                    projectClassRecord.setClassId(c.getClassId());
                    projectClassRecord.setProjectId(projectId);
                    projectClassRecord.setTeacherId(teacherId);
                    projectClassRecord.setCreateTime(TimeUtil.getCurrentTime());
                    if (Boolean.valueOf(classSplit[1])) {
                        projectClassRecord.setStatus("1");
                    } else {
                        projectClassRecord.setStatus("0");
                    }
                    projectClassRecordMapper.insert(projectClassRecord);
                }
            }
        return new ResultBean(true, "更新成功");
    }

    @Override
    public Project getProjectById(Integer projectId) {
        return projectMapper.selectByPrimaryKey(projectId);
    }

    @Override
    public Project getProjectWithTimesById(Integer projectId, Integer studentId) {
        return projectMapper.selectByPrimaryKeyWithTimes(projectId, studentId);
    }

    @Override
    public Object getProjectPageStudentCanSee(Integer pageNum, Integer classId, String keyword) {
        Class c = classMapper.selectByPrimaryKey(classId);


        if (pageNum < 1) pageNum = 1;
        PageBean<Project> pageBean = new PageBean<>();
        pageBean.setPageNum(pageNum);
        List<Project> res = projectMapper.getPageContentStudentCanSee(pageBean.getStart(), pageBean.getSize(), keyword, c.getTeacherId(), "1", classId);
        pageBean.setRowCount(projectMapper.getCountStudentCanSee(keyword, c.getTeacherId(), "1", classId));
        pageBean.setContent(res);
        return pageBean;
    }
}
